package flst.ipiweb.struts.action.article;

import ipiwebprocess.ArticleProcess;
import ipiwebprocess.EnseigneProcess;
import ipiwebtransverse.Article;
import ipiwebtransverse.Enseigne;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class ArticleFormulaire extends Action {

	private static ArticleProcess sArticleProcess = new ArticleProcess();
	private static EnseigneProcess sEnseigneProcess = new EnseigneProcess();

	public ActionForward execute(ActionMapping mapping, 
			ActionForm form,
			HttpServletRequest request, 
			HttpServletResponse response)
			throws Exception {

		
		List<Enseigne> listeEnseigne  = sEnseigneProcess.listerEnseigne();

		if ((request.getParameter("referenceArticle") != null)) {
			// On affiche le formulaire de modification
			Article vArticle = sArticleProcess.GetArticleById(new Integer(request.getParameter("referenceArticle")).intValue());
			ArticleForm vArticleForm = new ArticleForm();
			
			vArticleForm.setReferenceArticle(vArticle.getReference());
			vArticleForm.setDescriptionCourteArticle(vArticle.getDescription());
			vArticleForm.setModeleArticle(vArticle.getModele());
			vArticleForm.setQuantiteStockArticle(vArticle.getQuantiteStock());
			vArticleForm.setDescriptionLongueArticle(vArticle.getDescriptionTechnique());
			vArticleForm.setPrixArticle(vArticle.getPrix());
			vArticleForm.setImageArticle(vArticle.getImage());
			vArticleForm.setId(1);
			
			
			request.setAttribute("article", vArticleForm);
			request.setAttribute("listeEnseigne", listeEnseigne);
			request.setAttribute("retour", 1);
			
		} else {
			request.setAttribute("listeEnseigne", listeEnseigne);
			request.setAttribute("retour", 0);
		}

		if ((request.getParameter("generer") != null)) {
			int id = sArticleProcess.GenererReferenceArticle();
			request.setAttribute("articleId", id);
		}

		return mapping.findForward("article_formulaire");
		
	}
}
